package org.zht.dormpro.dorm.controller;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.DormDTO;
import org.zht.dormpro.dorm.model.TbDorm;
import org.zht.dormpro.dorm.model.TbFloor;
import org.zht.dormpro.dorm.service.IDormService;
import org.zht.dormpro.dorm.service.IFloorService;
import org.zht.dormpro.pub.ResponseMap;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
@RequestMapping("/dorm/dorm")
public class DormController {

    @Autowired
    private IDormService dormService;
    @Autowired
    private IFloorService floorService;

    /**
     * 跳转到宿舍列表页面
     * @param model
     * @return
     */
    @RequestMapping("/dormPage")
    public String dormPage(Model model){
        List<TbFloor> floors = floorService.getAllFloor(0);
        model.addAttribute("floors",floors);

        return "/dorm/dorm_list";
    }

    /**
     * 显示宿舍信息列表
     * @param params
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/listDorm")
    @ResponseBody
    @RequiresPermissions("dorm:list:all")
    public ResponseMap listDorm(@RequestParam Map<String,Object> params) throws UnsupportedEncodingException {
        String dormName = new String(params.get("dormName").toString().getBytes("iso-8859-1"),"utf-8");
        params.put("dormName",dormName);

        PageBean pageBean = new PageBean(params);
        List<DormDTO> dormDTOS = dormService.getDormByCondition(pageBean);
        Map<String,Object> attr = new HashMap<>();
        attr.put("total",pageBean.getTotal());
        return ResponseMap.success(dormDTOS,attr);
    }

    /**
     * 跳转到宿舍添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addDormPage")
    public String addDormPage(Model model){
        List<TbFloor> floors = floorService.getAllFloor(0);
        model.addAttribute("floors",floors);

        return "/dorm/dorm_add";
    }

    /**
     * 添加宿舍信息
     * @param dormDTO
     * @return
     */
    @RequestMapping(value = "/addDorm",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addDorm(DormDTO dormDTO){
        boolean success = dormService.addDorm(dormDTO);
        if(success){
            return ResponseMap.success("添加宿舍成功");
        }else {
            return ResponseMap.error("添加宿舍失败");
        }
    }

    /**
     * 跳转到宿舍编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/dormEditPage/{id}")
    public String dormEditPage(Model model,@PathVariable("id") Integer id){
        TbDorm dorm = dormService.getDormById(id);
        List<TbFloor> floors = floorService.getAllFloor(0);
        model.addAttribute("dorm",dorm);
        model.addAttribute("floors",floors);

        return "/dorm/dorm_edit";
    }

    /**
     * 编辑宿舍信息
     * @param dormDTO
     * @return
     */
    @RequestMapping(value = "/editDorm",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editDorm(DormDTO dormDTO){
        boolean success = dormService.editDorm(dormDTO);
        if(success){
            return ResponseMap.success("宿舍编辑成功");
        }else {
            return ResponseMap.error("宿舍编辑失败");
        }
    }

    /**
     * 删除宿舍信息
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteDorm",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteDorm(Integer id){
        boolean success = dormService.deleteDorm(id);
        if (success){
            return ResponseMap.success("删除宿舍成功");
        }else {
            return ResponseMap.error("删除宿舍失败");
        }
    }

    @RequestMapping(value = "batchRemoveDorm",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveDorm(@RequestParam("dormIds[]")String[] dormIds){
        boolean success = dormService.batchRemoveDorm(new ArrayList<>(Arrays.asList(dormIds)));
        if (success){
            return ResponseMap.success("批量删除宿舍成功");
        }else {
            return ResponseMap.error("批量删除宿舍失败");
        }
    }
}
